using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Utils;
using System;
using System.IO;
using System.IO.IsolatedStorage;
namespace MicroMsg.Storage
{
	public class StorageHash
	{
		private const string TAG = "StorageHash";
		private int dirNum;
		private string pathBase;
		public string fileExt = ".jpg";
		public void setParam(int int_0, string path)
		{
			if (string.IsNullOrEmpty(path) || int_0 <= 0)
			{
				DebugEx.debugBreak();
			}
			this.dirNum = int_0;
			this.pathBase = path;
		}
		private bool checkParam()
		{
			if (!string.IsNullOrEmpty(this.pathBase) && this.dirNum > 0)
			{
				return true;
			}
			DebugEx.debugBreak();
			return false;
		}
		public virtual bool InvalidFileName(string keyName)
		{
			return Util.InvalidFileName(keyName);
		}
		private static bool isNumbers(char char_0)
		{
			return char_0 >= '0' && char_0 <= '9';
		}
		public virtual int HashNum(string keyName)
		{
			if (string.IsNullOrEmpty(keyName))
			{
				return 0;
			}
			if (keyName.get_Chars(0) >= 'a' && keyName.get_Chars(0) <= 'z')
			{
				return (int)(keyName.get_Chars(0) - 'a');
			}
			if (keyName.get_Chars(0) >= 'A' && keyName.get_Chars(0) <= 'Z')
			{
				return (int)(keyName.get_Chars(0) - 'A');
			}
			if (StorageHash.isNumbers(keyName.get_Chars(0)) && StorageHash.isNumbers(keyName.get_Chars(1)))
			{
				return Convert.ToInt32(keyName.Substring(0, 2));
			}
			return Util.CheckSum(keyName);
		}
		public virtual string getHashPath(string keyName)
		{
			if (string.IsNullOrEmpty(keyName))
			{
				return null;
			}
			if (this.InvalidFileName(keyName))
			{
				keyName = MD5Core.GetHashString(keyName);
			}
			if (this.dirNum == 1)
			{
				return string.Format("{0}/{1}{2}", this.pathBase, keyName, this.fileExt);
			}
			return string.Format("{0}/{1}/{2}{3}", new object[]
			{
				this.pathBase,
				this.HashNum(keyName) % this.dirNum,
				keyName,
				this.fileExt
			});
		}
		public bool write(string keyName, params byte[][] dataList)
		{
			if (!string.IsNullOrEmpty(keyName) && dataList != null && this.checkParam())
			{
				try
				{
					using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
					{
						string hashPath = this.getHashPath(keyName);
						string directoryName = Path.GetDirectoryName(hashPath);
						if (!userStoreForApplication.DirectoryExists(directoryName))
						{
							userStoreForApplication.CreateDirectory(directoryName);
						}
						using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(hashPath, 2, userStoreForApplication))
						{
							for (int i = 0; i < dataList.Length; i++)
							{
								byte[] array = dataList[i];
								isolatedStorageFileStream.Write(array, 0, array.Length);
							}
						}
					}
					return true;
				}
				catch (Exception ex)
				{
					Log.smethod_1("StorageHash", string.Concat(new object[]
					{
						"write file Exception ",
						keyName,
						",Exception=",
						ex
					}));
					DebugEx.debugBreak();
				}
				return false;
			}
			return false;
		}
		public byte[] read(string keyName)
		{
			if (!string.IsNullOrEmpty(keyName) && this.checkParam())
			{
				try
				{
					using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
					{
						string hashPath = this.getHashPath(keyName);
						byte[] result;
						if (userStoreForApplication.FileExists(hashPath))
						{
							using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(hashPath, 3, userStoreForApplication))
							{
								if (isolatedStorageFileStream.get_Length() <= 0L)
								{
									result = null;
									return result;
								}
								byte[] array = new byte[isolatedStorageFileStream.get_Length()];
								int num = isolatedStorageFileStream.Read(array, 0, array.Length);
								if ((long)num != isolatedStorageFileStream.get_Length())
								{
									Log.smethod_1("StorageHash", string.Concat(new object[]
									{
										"read File error, read count = ",
										isolatedStorageFileStream.get_Length(),
										" , ret = ",
										num
									}));
									result = null;
									return result;
								}
								result = array;
								return result;
							}
						}
						result = null;
						return result;
					}
				}
				catch (Exception ex)
				{
					Log.smethod_1("StorageHash", string.Concat(new object[]
					{
						"read file Exception ",
						keyName,
						" ",
						ex
					}));
				}
				return null;
			}
			return null;
		}
		public bool exists(string keyName)
		{
			if (!string.IsNullOrEmpty(keyName) && this.checkParam())
			{
				try
				{
					using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
					{
						return userStoreForApplication.FileExists(this.getHashPath(keyName));
					}
				}
				catch (Exception ex)
				{
					Log.smethod_1("StorageHash", string.Concat(new object[]
					{
						"FileExists Exception ",
						keyName,
						"Exception=",
						ex
					}));
				}
				return false;
			}
			return false;
		}
		public void testAddFile(string path, int fileNum)
		{
			using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
			{
				for (int i = 0; i < fileNum; i++)
				{
					using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(string.Concat(new object[]
					{
						path,
						"/",
						i,
						".bin"
					}), 2, userStoreForApplication))
					{
						byte[] array = new byte[100];
						array[0] = (byte)i;
						isolatedStorageFileStream.Write(array, 0, array.Length);
					}
				}
			}
		}
		public static void test()
		{
			using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
			{
				userStoreForApplication.CreateDirectory("test");
				StorageHash storageHash = new StorageHash();
				storageHash.setParam(30, "test");
				string hashPath = storageHash.getHashPath("7s/00Uth+IPeSbcmFsqukUJTCMgaBVLH3nDtkDuMk9T@bottle:7s/00Uth+IPKGZvOxkDqBrWAVUBGVFk13j9vaDDK5wqzKxRgaBpcOTlv6Luhayab");
				Path.GetDirectoryName(hashPath);
				DebugEx.getTimeSpan();
				byte[] array = new byte[100];
				array[0] = 1;
				for (int i = 0; i < 30; i++)
				{
				}
				Log.smethod_5("test", "add 300 file time=" + DebugEx.getTimeSpan());
				string[] fileNames = userStoreForApplication.GetFileNames("test/*");
				string[] array2 = fileNames;
				for (int j = 0; j < array2.Length; j++)
				{
					string text = array2[j];
					userStoreForApplication.DeleteFile("test/" + text);
				}
				Log.smethod_5("test", string.Concat(new object[]
				{
					"del  300 file time=",
					DebugEx.getTimeSpan(),
					"count=",
					fileNames.Length
				}));
				Log.smethod_5("test", "end");
			}
		}
	}
}
